from django.shortcuts import render, HttpResponse

# Create your views here.

def book_list(request):
    book = models.Book.objects.all()
    return render(request, 'book_list.html', {'book_list': book})

def add_book(request):
    if request.method == 'POST':
        new_book_name = request.POST.get('name')
        new_book_id = request.POST.get('id')
        new_book_author = request.POST.get('author')
        new_book_translater = request.POST.get('translater')
        new_book_publisher = request.POST.get('publisher')
        models.Book.objects.creat(name=new_book_name, id=new_book_id, author=new_book_author,
                                  translater=new_book_translater, publisher=new_book_publisher)
        return redirect('/book_list')

def drop_book(request):
    drop_id = request.GET.get('id')
    drop_obj = models.Book.objects.get(id=drop_id)
    drop_obj.delete()
    return redirect('/book_list/')

def edit_book(request):
    if request.method == 'POST':
        new_book_name = request.POST.get('name')
        new_book_id = request.POST.get('id')
        new_book_author = request.POST.get('author')
        new_book_translator = request.POST.get('translator')
        new_book_publisher = request.POST.get('publisher')
        edit_id = request.GET.get('id')
        edit_obj = models.Book.objects.get(id=edit_id)
        edit_obj.name = new_book_name
        edit_obj.ISBN = new_book_id
        edit_obj.translator = new_book_translator
        edit_obj.author = new_book_author
        edit_obj.publisher = new_book_publisher
        edit_obj.save()
        return redirect('/book_list/')
    edit_id = request.GET.get('id')
    edit_obj = models.Book.objects.get(id=edit_id)
    all_publisher = models.Publisher.objects.all()
    return render(request, 'book_edit.html', {'book': edit_obj, 'publisher_list': all_publisher})

